using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Octopus.Logging;
using System.Diagnostics;

namespace Octopus.Logging.Impl.Text
{
    public class StandardFormatter : IFormatter
    {
        int PID = Process.GetCurrentProcess().Id;

        private readonly string sConn = " - ";

        public string DoFormat(object sender,string title, DateTime logTime, int threadID, ActionState actionState, string message, Exception err, IDictionary<string, object> context)
        {
            StringBuilder sb = new StringBuilder("[");
            sb.Append(logTime.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
            sb.Append(sConn);
            sb.Append(title);
            sb.Append(sConn);
            sb.Append(sender);
            sb.Append(sConn);
            sb.Append(PID);
            sb.Append(sConn);
            sb.Append(threadID.ToString());
            sb.Append(sConn);
            sb.Append(actionState.ToString());
            sb.AppendLine("]");

            if (!string.IsNullOrEmpty(message))
            {
                sb.Append("Message:");
                sb.AppendLine(message);
            }
            if (err != null)
            {
                sb.Append("Exception:");
                sb.AppendLine(err.ToString());
                if (err.InnerException != null)
                {
                    sb.Append("InnerException:");
                    sb.AppendLine(err.InnerException.ToString());
                }
            }
            if (context != null && context.Count > 0)
            {
                foreach (KeyValuePair<string, object> item in context)
                {
                    sb.Append(item.Key);
                    sb.Append(':');
                    sb.AppendLine(item.Value==null ? null : item.Value.ToString());
                }
            }
            sb.AppendLine();
            return sb.ToString();
        }
    }
}
